//
//  SearchRecordViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "SearchRecordViewController.h"
#import "UIView+Utils.h"
#import "ShopAdornViewController.h"
//cell
#import "ServeCollectionCell.h"
#import "ShopAttentionCell.h"
#import "CaseTableViewCell.h"

//
#import "FPCommodityDetailVC.h"
#import "CaseDetailsViewController.h"
#import "ShopAdornViewController.h"

#import "ServeGoodsModel.h"
#import "DesignListModel.h"
#import "CreateAreaModel.h"
#import <MLMenu/MLMenuView.h>
//

@interface SearchRecordViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>

// topBarView 底部的 线
@property (nonatomic, strong) UIView        * moveLine;
@property (nonatomic, strong) UIView        * topBarView; // tag  1000
@property (nonatomic, strong) UIView        * sortBarView;// tag  2000
@property (nonatomic, strong) UIScrollView  * scrollView; // tag  3000


/// 全部
//@property (nonatomic,strong)  NSArray          * allListDataArray;
/// 设计师
@property (nonatomic,strong)  NSArray          * designListDataArray;
/// 商品
@property (nonatomic,strong)  NSArray          * serveListDataArray;
/// 创意空间
@property (nonatomic,strong)  NSArray          * createAreaDataArray;

/// 请求 接口初始值 综合、价格、区域
@property (nonatomic,copy)    NSString         * orderBy_pa;
/// 筛选类型
@property (nonatomic,copy)    NSString         * label_pa;


@property (nonatomic,strong)  ServeGoodsModel  * serveGoodsModel;
@property (nonatomic,strong)  DesignListModel  * designListModel;
@property (nonatomic,strong)  CreateAreaModel  * createAreaModel;


@property (nonatomic, strong) UIButton     *selectBtn;
@property (nonatomic, strong) UIButton     *sortBtn;

@property (nonatomic, strong) UITextField  *searchField;

@property (nonatomic,assign) NSInteger  scrollIndex;

@property (nonatomic,strong)  MLMenuView       * sortTypeView;


@end

@implementation SearchRecordViewController

//- (NSArray *)allListDataArray
//{
//    if (_allListDataArray) {
//        _allListDataArray = [NSArray array];
//    }
//    return _allListDataArray;
//}

- (NSArray *)designListDataArray
{
    if (_designListDataArray == nil) {
        _designListDataArray = [[NSArray alloc] init];
    }
    return _designListDataArray;
}

- (NSArray *)serveListDataArray
{
    if (_serveListDataArray == nil) {
        _serveListDataArray = [[NSArray alloc] init];
    }
    return _serveListDataArray;
}

- (NSArray *)createAreaDataArray
{
    if (_createAreaDataArray == nil) {
        _createAreaDataArray = [[NSArray alloc] init];
    }
    return _createAreaDataArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavBar];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setTopBar];
    [self setSortBarViewUI];
    [self setUI];
    
    
    [self initDefaultData];
    [self updateViewUI];

    
//    [self refreshTableView:[self.scrollView viewWithTag:self.selectIndex + 200]];
}

- (void)initDefaultData
{
    self.scrollIndex = -1;
    self.orderBy_pa = @"default desc";
    self.label_pa = @"彩印设计";
}

- (void)updateViewUI
{
    [self updateTopBarAndSortBarViewWithSelectIndex];
    [self getDataWithSelectIndex];
}

- (void)updateTopBarAndSortBarViewWithSelectIndex
{
    if (self.selectIndex == 0) {
        self.sortBarView.hidden = YES;
        self.scrollView.frame = CGRectMake(0, ScreenX375(30), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(30));
    }else{
        self.sortBarView.hidden = false;
        self.scrollView.frame = CGRectMake(0, ScreenX375(60), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(60));
        
        if (self.selectIndex == 1) {
            [self setSortBarViewWith:@[@"综合", @"成交", @"好评", @"区域", @"筛选"]];
        }else{
            [self setSortBarViewWith:@[@"综合", @"价格", @"区域", @"筛选"]];
        }
    }
}

- (void)getDataWithSelectIndex
{
    if (self.selectIndex == 0) {
        [self getAllDataFromServer];
    }else{
        BaseTableView * baseTableView = [self.scrollView viewWithTag:3000+self.selectIndex];
        [baseTableView.mj_header beginRefreshing];
    }
}

// 请求所有数据
- (void)getAllDataFromServer
{
    // 请求 3001 数据
    [HttpManager showWait];
    [self getAllDataWithTag:3001 success:^(id response) {
        // 处理 3001 的数据
        BOOL  result = [self handleAllData:response tag:3001];
        if (result) {
            // 请求 3002 数据
            [self getAllDataWithTag:3002 success:^(id response) {
                // 处理 3002 的数据
                BOOL  result = [self handleAllData:response tag:3002];
                if (result) {
                    // 请求 3003 数据
                    [self getAllDataWithTag:3003 success:^(id response) {
                        // 处理 3003 的数据
                        BOOL  result = [self handleAllData:response tag:3003];
                        /// 刷新
                        [HttpManager hideWait];
                        if (result) {
//                            self.allListDataArray = @[self.designListDataArray, self.serveListDataArray, self.createAreaDataArray];
                            BaseTableView * baseTableView = [self.scrollView viewWithTag:3000];
                            [baseTableView reloadData];
                        }
                    }];
                }
            }];
        }
    }];
}

/// 解析所有数据
- (BOOL)handleAllData:(id)response tag:(NSInteger)tag
{
    NSDictionary * dic = response;
    if (dic == nil) {
        [HttpManager hideWait];
        return NO;
    }
    NSString *status = [NSString stringWithFormat:@"%@",[dic valueForKey:@"status"]];
    
    if ([status isEqualToString:@"1"]) {
        [self handelAllDataArrayWith:tag dict:dic[@"data"]];
        return YES;
    }else{
        [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",response[@"msg"]]];
        return NO;
    }
}


- (void)handelAllDataArrayWith:(NSInteger)tag dict:(NSDictionary*)dict
{
    NSError *error;
    if (tag == 3001) {
        DesignListModel *designListModel = [[DesignListModel alloc] initWithDictionary:dict error:&error];
        self.designListModel = designListModel;
        if (designListModel != nil) {
            self.designListDataArray = designListModel.data;
        }
    }else if (tag == 3002){
        ServeGoodsModel *serveGoodsModel = [[ServeGoodsModel alloc] initWithDictionary:dict error:&error];
        self.serveGoodsModel = serveGoodsModel;
        if (serveGoodsModel != nil) {
            self.serveListDataArray = serveGoodsModel.data;
        }
    }else{
        CreateAreaModel *createAreaModel = [[CreateAreaModel alloc] initWithDictionary:dict error:&error];
        self.createAreaModel = createAreaModel;
        if (createAreaModel != nil) {
            self.createAreaDataArray = createAreaModel.data;
        }
    }
}

- (void)getAllDataWithTag:(NSInteger)tag success:(void (^)(id))successBlock
{
    NSDictionary *dict = [self postParameterWithTag:tag isMore:NO];
    if (dict == nil) {
        return;
    }
    NSString * url = [self postUrlWithTag:tag];
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        if (successBlock) {
            successBlock(successResponse);
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

#pragma mark - 刷新数据 请求数据 设计师3001 / 商品3002 / 创意空间3003
- (NSDictionary *)postParameterWithTag:(NSInteger)tag isMore:(BOOL)isMore
{
    NSDictionary * dict = @{};
    if (tag == 3001) {
        if (isMore) {
            if (self.designListModel.current_page != nil) {
                int page = [self.designListModel.current_page intValue]+1;
                if (_isPrefixDesign) {
                    dict=@{@"page":@(page),
                           @"orderBy":self.orderBy_pa,
                           @"label":self.labels,
                           @"longitude":kNSUDefaultReadKey(@"longitude"),
                           @"latitude":kNSUDefaultReadKey(@"latitude")};
                }
                dict=@{@"page":@(page),
                       @"orderBy":self.orderBy_pa,
                       @"label":self.label_pa,
                       @"longitude":kNSUDefaultReadKey(@"longitude"),
                       @"latitude":kNSUDefaultReadKey(@"latitude")};
            }else{
                return nil;
            }
        }else{
            if (_isPrefixDesign) {
                dict=@{@"page":@1,
                       @"orderBy":self.orderBy_pa,
                       @"label":self.labels,
                       @"longitude":kNSUDefaultReadKey(@"longitude"),
                       @"latitude":kNSUDefaultReadKey(@"latitude")};
            }
            dict=@{@"page":@1,
                   @"orderBy":self.orderBy_pa,
                   @"label":self.label_pa,
                   @"longitude":kNSUDefaultReadKey(@"longitude"),
                   @"latitude":kNSUDefaultReadKey(@"latitude")};
        }
        
    }else if (tag == 3002){
        if (isMore) {
            if (self.serveGoodsModel.current_page != nil) {
                int page = [self.serveGoodsModel.current_page intValue]+1;
                if (_isPrefixDesign) {
                    dict=@{@"page":@(page),@"orderBy":self.orderBy_pa,@"label":self.labels};

                }
                dict=@{@"page":@(page),@"orderBy":self.orderBy_pa,@"label":self.label_pa};
            }else{
                return nil;
            }
        }else{
            if (_isPrefixDesign) {
                dict=@{@"page":@1,@"orderBy":self.orderBy_pa,@"label":self.labels};
            }
            dict=@{@"page":@1,@"orderBy":self.orderBy_pa,@"label":self.label_pa};
        }
    }else{
        if (isMore) {
            if (self.createAreaModel.current_page != nil) {
                int page = [self.createAreaModel.current_page intValue]+1;
                if (_isPrefixDesign) {
                    dict=@{@"page":@(page),@"orderBy":self.orderBy_pa,@"label":self.labels};
                }
                dict=@{@"page":@(page),@"orderBy":self.orderBy_pa,@"label":self.label_pa};
            }else{
                return nil;
            }
        }else{
            if (_isPrefixDesign) {
                dict=@{@"page":@1,@"orderBy":self.orderBy_pa,@"label":self.labels};
            }
            dict=@{@"page":@1,@"orderBy":self.orderBy_pa,@"label":self.label_pa};
        }
    }
    return dict;
}

- (NSString *)postUrlWithTag:(NSInteger)tag
{
    NSString * url = @"";
    if (tag == 3001) {
        url = [NSString stringWithFormat:@"%@app/shop_index",REQUESTHEADER];
    }else if (tag == 3002){
        url = [NSString stringWithFormat:@"%@app/service_index",REQUESTHEADER];
    }else{
        url = [NSString stringWithFormat:@"%@app/case_index",REQUESTHEADER];
    }
    return url;
}

- (void)getDataFromServerWithTag:(NSInteger)tag
{
    NSDictionary *dict = [self postParameterWithTag:tag isMore:NO];
    if (dict == nil) {
        BaseTableView * baseTableView = [self.scrollView viewWithTag:tag];
        [baseTableView.mj_footer endRefreshing];
        return;
    }
    NSString * url = [self postUrlWithTag:tag];
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        
        [selfWeak handleDataWith:tag successResponse:successResponse isMore:NO];
       
    } andFailure:^(id failureResponse) {
        BaseTableView * baseTableView = [selfWeak.scrollView viewWithTag:tag];
        [baseTableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}
// 解析数据
- (void)handleDataWith:(NSInteger)tag successResponse:(id)successResponse isMore:(BOOL)isMore
{
    BaseTableView * baseTableView = [self.scrollView viewWithTag:tag];
    NSDictionary * dic = successResponse;
    if (dic == nil) {
        if (isMore) {
            [baseTableView.mj_footer endRefreshing];
        }else{
            [baseTableView.mj_header endRefreshing];
        }
        return;
    }
    NSString *status = [NSString stringWithFormat:@"%@",[dic valueForKey:@"status"]];
    
    if ([status isEqualToString:@"1"]) {
        if (isMore) {
            [baseTableView.mj_footer endRefreshing];
        }else{
            [baseTableView.mj_header endRefreshing];
        }
        [self handelDataArrayWith:tag dict:dic[@"data"] isMore:isMore];
        [baseTableView reloadData];
    }else{
        [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
    }
}
/// 处理 三种 数据源 - 数组
- (void)handelDataArrayWith:(NSInteger)tag dict:(NSDictionary*)dict isMore:(BOOL)isMore
{
    NSError *error;
    
    if (tag == 3001) {
        DesignListModel *designListModel = [[DesignListModel alloc] initWithDictionary:dict error:&error];
        self.designListModel = designListModel;
        if (designListModel != nil) {
            if (isMore) {
                self.designListDataArray = [self.designListDataArray arrayByAddingObjectsFromArray:designListModel.data];
            }else{
                self.designListDataArray = designListModel.data;
            }
        }
    }else if (tag == 3002){
        ServeGoodsModel *serveGoodsModel = [[ServeGoodsModel alloc] initWithDictionary:dict error:&error];
        self.serveGoodsModel = serveGoodsModel;
        if (serveGoodsModel != nil) {
            if (isMore) {
                self.serveListDataArray = [self.serveListDataArray arrayByAddingObjectsFromArray:serveGoodsModel.data];
            }else{
                self.serveListDataArray = serveGoodsModel.data;
            }
        }
    }else{
        CreateAreaModel *createAreaModel = [[CreateAreaModel alloc] initWithDictionary:dict error:&error];
        self.createAreaModel = createAreaModel;
        if (createAreaModel != nil) {
            if (isMore) {
                self.createAreaDataArray = [self.createAreaDataArray arrayByAddingObjectsFromArray:createAreaModel.data];
            }else{
                self.createAreaDataArray = createAreaModel.data;
            }
        }
    }
}

#pragma mark - 更多数据 请求数据 设计师3001 / 商品3002 / 创意空间3003
- (void)getMoreDataFromServerWithTag:(NSInteger)tag
{
    NSDictionary *dict = [self postParameterWithTag:tag isMore:YES];
    if (dict == nil) {
        BaseTableView * baseTableView = [self.scrollView viewWithTag:tag];
        [baseTableView.mj_footer endRefreshing];
        return;
    }
    NSString * url = [self postUrlWithTag:tag];
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        
        [selfWeak handleDataWith:tag successResponse:successResponse isMore:YES];
        
    } andFailure:^(id failureResponse) {
        BaseTableView * baseTableView = [selfWeak.scrollView viewWithTag:tag];
        [baseTableView.mj_footer endRefreshing];
        [HttpManager showFail];
    }];
    
}



- (void)setNavBar {
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 14, 44)];
    [backBtn setImage:[UIImage imageNamed:@"返回白色"] forState:UIControlStateNormal];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 52, 30)];
    UIImageView *leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 4, 22, 22)];
    leftImg.image = [UIImage imageNamed:@"搜索"];
    [leftView addSubview:leftImg];
    
    _searchField = [[UITextField alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH - ScreenX375(54), 30)];
    _searchField.placeholder = @"找专业的人才做专业的事情";
    _searchField.layer.cornerRadius = ScreenX375(15);
    _searchField.clipsToBounds = YES;
    _searchField.backgroundColor = [UIColor whiteColor];
    _searchField.leftViewMode = UITextFieldViewModeAlways;
    _searchField.leftView = leftView;
    _searchField.font = Kfont(13);
    _searchField.returnKeyType = UIReturnKeySearch;
    _searchField.delegate = self;
    self.navigationItem.titleView = _searchField;
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)typeClick:(UIButton *)button
{
    _selectBtn.selected = NO;
    button.selected = YES;
    _selectBtn = button;
    [UIView animateWithDuration:0.3 animations:^{
        self.moveLine.frame = CGRectMake((SCREEN_WIDTH / 4 - ScreenX375(30)) / 2 + (button.tag - 1000) * SCREEN_WIDTH / 4, ScreenX375(30) - 2, ScreenX375(30), 2);
    }];
    [_scrollView setContentOffset:CGPointMake((button.tag - 1000) * SCREEN_WIDTH, 0) animated:YES];
}


- (void)setTopBar {
    self.topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(30))];
    self.topBarView.backgroundColor = RGB_COLOR(235, 67, 90);
    [self.view addSubview:self.topBarView];
    
    NSArray *nameArr = @[@"全部",@"设计师",@"商品",@"创意空间"];
    for (int i = 0; i < nameArr.count; i++) {
        UIButton *typeBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH / 4, 0, SCREEN_WIDTH / 4, ScreenX375(30))];
        [typeBtn setTitle:nameArr[i] forState:UIControlStateNormal];
        [typeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [typeBtn setTitleColor:RGBACOLOR(255, 255, 255, 0.6) forState:UIControlStateNormal];
        typeBtn.titleLabel.font = Kfont(16);
        typeBtn.tag = 1000 + i;
        if (i == 0) {
            typeBtn.selected = YES;
            _selectBtn = typeBtn;
        }
        
        [typeBtn addTarget:self action:@selector(typeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.topBarView addSubview:typeBtn];
    }
    
    self.moveLine = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH / 4 - ScreenX375(30)) / 2, ScreenX375(30) - 2, ScreenX375(30), 2)];
    self.moveLine.backgroundColor = [UIColor whiteColor];
    [self.topBarView addSubview:self.moveLine];
}


- (void)setSortBarViewUI
{
    self.sortBarView = [[UIView alloc] init];
    self.sortBarView.frame = CGRectMake(0, ScreenX375(30), SCREEN_WIDTH, ScreenX375(30));
    self.sortBarView.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    [self.view addSubview:self.sortBarView];
    
}

- (void)setSortBarViewWith:(NSArray *)titles
{
    [self.sortBarView removeAllSubviews];
    for (int i = 0; i < titles.count; i++) {
        UIButton *sortBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH / 4, 0, SCREEN_WIDTH / 4, ScreenX375(30))];
        [sortBtn setTitle:titles[i] forState:UIControlStateNormal];
        [sortBtn setTitleColor:ThemeColor forState:UIControlStateSelected];
        [sortBtn setTitleColor:REXADECIMALCOLOR(@"#999999") forState:UIControlStateNormal];
        sortBtn.titleLabel.font = Kfont(14);
        sortBtn.tag = 2000 + i;
        if (i == 0) {
            sortBtn.selected = YES;
            _sortBtn = sortBtn;
        }
        [sortBtn addTarget:self action:@selector(sortClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.sortBarView addSubview:sortBtn];
    }
}

- (void)sortClick:(UIButton *)button {
    _sortBtn.selected = NO;
    button.selected = YES;
    _sortBtn = button;
    [self setSortTypeUI:button];
    
}

-(void)setSortTypeUI:(UIButton *)button
{
    NSArray *titlesAry = @[];
    switch (button.tag) {
        case 2000:
            titlesAry = @[@"综合升序",@"综合降序"];
            break;
        case 2001:
            if (self.selectIndex == 1) {
                titlesAry = @[@"成交升序",@"成交降序"];
            }else{
                titlesAry = @[@"价格升序",@"价格降序"];
            }
            break;
        case 2002:
            if (self.selectIndex == 1) {
                titlesAry = @[@"好评升序",@"好评降序"];
            }else{
                titlesAry = @[@"距离升序",@"距离降序"];
            }
            break;
        case 2003:
            titlesAry = @[@"彩印设计",@"水印设计",@"数码设计",@"结构设计"];
            break;
        case 2004:
            titlesAry = @[@"彩印设计",@"水印设计",@"数码设计",@"结构设计"];
            break;
    }
   
    self.sortTypeView = [[MLMenuView alloc] initWithFrame:CGRectMake(button.left, ScreenX375(60), ScreenX375(103.5), ScreenX375(176)) WithTitles:titlesAry WithImageNames:nil WithMenuViewOffsetTop:NAVIGATION_BAR_HEIGHT + 60 ];
    self.sortTypeView.separatorOffSet = 10;
    self.sortTypeView.separatorColor = RGB_COLOR(220,220,220);
    [self.sortTypeView setMenuViewBackgroundColor:[UIColor whiteColor]];
    self.sortTypeView.titleColor = FONTCOLOR_LIGHTGRAY;
    [self.sortTypeView setCoverViewBackgroundColor:RGBACOLOR(51, 51, 51, 0.5)];
    
     WeakObj(self);
    self.sortTypeView.didSelectBlock = ^(NSInteger index) {
        //selfWeak.sortTypeView.titleColor = ThemeColor;
        
        if (button.tag == 2003 || button.tag == 2004) {
            if (index == 0) {
                selfWeak.label_pa = @"彩印设计";
            }else if (index == 1){
                selfWeak.label_pa = @"水印设计";
            }else if(index == 2){
                selfWeak.label_pa = @"数码设计";
            }else{
                selfWeak.label_pa = @"结构设计";
            }
//            return;
        }else{
            switch (button.tag) {
                case 2000:
                    selfWeak.orderBy_pa = index == 0 ? @"default asc" : @"default desc";
                    break;
                case 2001:
                    //如果是设计师，就不是volume asc 或者 volume desc
                    if (self.selectIndex == 1) {
                        selfWeak.orderBy_pa = index == 0 ? @"default asc" : @"default desc";
                    }else{
                        selfWeak.orderBy_pa = index == 0 ? @"volume asc" : @"volume desc";
                    }
                    
                    break;
                case 2002:
                    selfWeak.orderBy_pa =  index == 0 ? @"default asc" : @"default desc";
                    break;
            }
        }
        [selfWeak updateViewUI];
        
//        [selfWeak refreshTableView:[selfWeak.scrollView viewWithTag:selfWeak.selectIndex + 200]];
        
    };
    [self.sortTypeView showMenuEnterAnimation:MLAnimationStyleNone];
    
}

- (void)setUI {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, ScreenX375(60), SCREEN_WIDTH, SCREEN_HEIGHT - ScreenX375(60))];
     self.scrollView .showsVerticalScrollIndicator = false;
     self.scrollView .showsHorizontalScrollIndicator = false;
     self.scrollView .pagingEnabled = true;
     self.scrollView .delegate = self;
     self.scrollView .contentSize = CGSizeMake(SCREEN_WIDTH * 4, 0);
     self.scrollView.tag = 2999;
    [self.view addSubview:self.scrollView];
    
    for (int i = 0; i < 4; i++) {
        BaseTableView * tableView = [[BaseTableView alloc] init];
        if (i == 0) {
            tableView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(30));
        }else{
            tableView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(60));
            [self setTableViewHeaderAndFooterRefresh:tableView];
        }
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.tag = 3000 + i;
        [self.scrollView addSubview:tableView];
    }
    [self.scrollView setContentOffset:CGPointMake(_selectIndex * SCREEN_WIDTH, 0) animated:YES];
}

- (void)setTableViewHeaderAndFooterRefresh:(BaseTableView *)tableView
{
    WeakObj(self);
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfWeak getDataFromServerWithTag:tableView.tag];
    }];
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [selfWeak getMoreDataFromServerWithTag:tableView.tag];
    }];
}
#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView.tag == 3000) {
        return 3;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 3002) {
        return self.serveListDataArray.count;
    }else if (tableView.tag == 3001) {
        return self.designListDataArray.count;
    }else if (tableView.tag == 3003){
        return self.createAreaDataArray.count;
    }else{
        if (section == 0) {
            return self.designListDataArray.count;
        }else if (section == 1){
            return self.serveListDataArray.count;
        }else{
            return self.createAreaDataArray.count;
        }
    }
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 3000) {
        if (indexPath.section == 0) {
            ShopAttentionCell *cell = [ShopAttentionCell cellWithTableView:tableView andIndexPath:indexPath];
            cell.designListDataModel = self.designListDataArray[indexPath.row];
            cell.collctBtn.hidden = YES;
            return cell;
        }else if (indexPath.section == 1) {
            ServeCollectionCell *cell = [ServeCollectionCell cellWithTableView:tableView andIndexPath:indexPath];
            cell.serveGoodsDataModel = self.serveListDataArray[indexPath.row];
            cell.collctBtn.hidden = YES;
            return cell;
        }else {
            CaseTableViewCell *cell = [CaseTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
            cell.createAreaDataModel = self.createAreaDataArray[indexPath.row];
            return cell;
        }
    }else if (tableView.tag == 3001) {
        ShopAttentionCell *cell = [ShopAttentionCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.designListDataModel = self.designListDataArray[indexPath.row];
        cell.collctBtn.hidden = YES;
        return cell;
    }else if (tableView.tag == 3002) {
        ServeCollectionCell *cell = [ServeCollectionCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.serveGoodsDataModel = self.serveListDataArray[indexPath.row];
        cell.collctBtn.hidden = YES;
        return cell;
    }else {
        CaseTableViewCell *cell = [CaseTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.createAreaDataModel = self.createAreaDataArray[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 3000) {
        if (indexPath.section == 0) {
            ShopAdornViewController *vc = [[ShopAdornViewController alloc] init];
            vc.shop_id = [self.designListModel.data valueForKey:@"Id"][indexPath.row];
            vc.isMine = NO;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.section == 1) {
            FPCommodityDetailVC *vc = [[FPCommodityDetailVC alloc] init];
            vc.design_id = [[self.serveGoodsModel.data valueForKey:@"Id"][indexPath.row] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            CaseDetailsViewController *vc = [[CaseDetailsViewController alloc] init];
            vc.design_id = [[self.createAreaModel.data valueForKey:@"Id"][indexPath.row] integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (tableView.tag == 3001) {
        ShopAdornViewController *vc = [[ShopAdornViewController alloc] init];
        vc.shop_id = [self.designListModel.data valueForKey:@"Id"][indexPath.row];
        vc.isMine = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tableView.tag == 3002) {
        FPCommodityDetailVC *vc = [[FPCommodityDetailVC alloc] init];
        vc.design_id = [[self.serveGoodsModel.data valueForKey:@"Id"][indexPath.row] integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        CaseDetailsViewController *vc = [[CaseDetailsViewController alloc] init];
        vc.design_id = [[self.createAreaModel.data valueForKey:@"Id"][indexPath.row] integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 3000) {
        if (indexPath.section == 0) {
            return ScreenX375(111);
        }else if (indexPath.section == 1) {
            return ScreenX375(101);
        }else {
            return ScreenX375(108);
        }
    }else if (tableView.tag == 3001) {
        return ScreenX375(111);
    }else if (tableView.tag == 3002) {
        return ScreenX375(101);
    }else {
        return ScreenX375(108);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView.tag == 3000) {
        return ScreenX375(53);
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (tableView.tag == 3000) {
        return ScreenX375(10);
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView.tag == 3000) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(53))];
        view.backgroundColor = [UIColor whiteColor];
        
        UIView *rline = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
        rline.backgroundColor = ThemeColor;
        [view addSubview:rline];
        
        UILabel *noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(15), ScreenX375(200), ScreenX375(24))];
        noteLabel.text = section == 0 ? @"设计师" : (section == 1 ? @"商品" : @"创意空间");
        noteLabel.textColor = REXADECIMALCOLOR(@"#333333");
        noteLabel.font = Kfont(18);
        [view addSubview:noteLabel];
        
        UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(60), 0, ScreenX375(60), ScreenX375(53))];
        [moreBtn setTitle:@"更多 >" forState:UIControlStateNormal];
        [moreBtn setTitleColor:REXADECIMALCOLOR(@"#666666") forState:UIControlStateNormal];
        moreBtn.tag = 1000 + section;
        moreBtn.titleLabel.font = Kfont(15);
        moreBtn.tag = 4000 + section;
        [moreBtn addTarget:self action:@selector(turnToMore:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:moreBtn];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(53) - 0.5, SCREEN_WIDTH, 0.5)];
        line.backgroundColor = LineColor;
        [view addSubview:line];
        
        return view;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (tableView.tag == 3000) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(10))];
        view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
        return view;
    }
    return nil;
}

- (void)turnToMore:(UIButton *)sender {
    
    NSInteger index = sender.tag - 4000 + 1;
    
    UIButton *button = [self.topBarView viewWithTag:index + 1000];
    _selectBtn.selected = NO;
    button.selected = YES;
    _selectBtn = button;
    [UIView animateWithDuration:0.3 animations:^{
        self.moveLine.frame = CGRectMake((SCREEN_WIDTH / 4 - ScreenX375(30)) / 2 + (button.tag - 1000) * SCREEN_WIDTH / 4, ScreenX375(30) - 2, ScreenX375(30), 2);
    }];
    [_scrollView setContentOffset:CGPointMake((button.tag - 1000) * SCREEN_WIDTH, 0) animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.tag == 2999) {
        
        NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
        
        if (index != self.scrollIndex) {
            UIButton *button = [self.topBarView viewWithTag:index + 1000];
            _selectBtn.selected = NO;
            button.selected = YES;
            _selectBtn = button;
            [UIView animateWithDuration:0.3 animations:^{
                self.moveLine.frame = CGRectMake((SCREEN_WIDTH / 4 - ScreenX375(30)) / 2 + (button.tag - 1000) * SCREEN_WIDTH / 4, ScreenX375(30) - 2, ScreenX375(30), 2);
            }];
            self.scrollIndex = index;
            self.selectIndex = index;
            [self updateViewUI];
        }
    }
}


@end
